package com.zwj.utils;

import com.zwj.entity.NodeDiscussEntity;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zxj
 * @Description: 树形评论
 * @DateTime: 2021/11/16 10:15
 */
public class DiscussTree {
    public static Map<String,Object> mapArray = new LinkedHashMap<String, Object>();
    public List<NodeDiscussEntity> menuCommon;
    public List<Object> list = new ArrayList<Object>();
    public List<Object> menuList(List<NodeDiscussEntity> menu){
        this.menuCommon = menu;
        for (NodeDiscussEntity x : menu) {
            Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
            if(x.getParentId()==0){
                mapArr.put("id", x.getId());
                mapArr.put("nodeId", x.getNodeId());
                mapArr.put("createBy", x.getCreateBy());
                mapArr.put("createTime", x.getCreateTime());
                mapArr.put("sortCode", x.getSortCode());
                mapArr.put("data", x.getData());
                mapArr.put("parentId", x.getParentId());
                mapArr.put("sortCode", x.getSortCode());
                mapArr.put("children", menuChild(x.getId()));
                list.add(mapArr);
            }
        }
        return list;
    }
    public List<?> menuChild(int id){
        List<Object> lists = new ArrayList<Object>();
        for(NodeDiscussEntity a:menuCommon){
            Map<String,Object> childArray = new LinkedHashMap<String, Object>();
            if(a.getParentId() == id){
                childArray.put("id", a.getId());
                childArray.put("nodeId", a.getNodeId());
                childArray.put("createBy", a.getCreateBy());
                childArray.put("createTime", a.getCreateTime());
                childArray.put("sortCode", a.getSortCode());
                childArray.put("data", a.getData());
                childArray.put("parentId", a.getParentId());
                childArray.put("sortCode", a.getSortCode());
                childArray.put("children", menuChild(a.getId()));
                lists.add(childArray);
            }
        }
        return lists;
    }
}
